﻿using System;
using System.Windows;
using System.Windows.Threading;
using zTalk.Network;

namespace zTalk
{
    public partial class ViewProfile : Window
    {
        public UserItem User { get; set; }

        public ViewProfile()
        {
            InitializeComponent();

            grInfo.Visibility = Visibility.Hidden;

            App.Handler.GetProfile += new PacketEventHandler(Handler_GetProfile);
        }

        void Handler_GetProfile(object sender, PacketEventArgs ea)
        {
            string[] data = (string[])ea.Result;

            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate {
                lblAbout.Content = User.About;
                lblUsername.Content = User.Username;
                imgAvatar.Source = App.DecodeAvatar(User.Avatar);

                if (data[0] == "error")
                {
                    errorPane.Visibility = Visibility.Visible;
                }
                else
                {
                    lblName.Content = data[1];
                    lblEmail.Content = data[2];
                    lblGender.Content = "Unspecified";
                    if (data[3] == "1")
                    {
                        lblGender.Content = "Male";
                    }
                    else if (data[3] == "2")
                    {
                        lblGender.Content = "Female";
                    }
                    grInfo.Visibility = Visibility.Visible;
                }
            }));
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Packet packet = Packet.Create(
                Opcodes.GET_PROFILE,
                Json.Encode<string[]>(new string[] { User.Username })
            );
            App.Client.SendPacket(packet);
        }
    }
}
